package com.moehu.back.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moehu.back.dao.TagDao;
import com.moehu.back.model.OpusModel;
import com.moehu.back.model.ResultModel;
import com.moehu.back.model.TagModel;
import com.moehu.back.model.request.CreatedOpusInfoTagModel;
import com.moehu.back.service.TagService;
import com.moehu.back.service.TokenService;
import com.moehu.back.utils.ResultUtils;
import com.moehu.back.utils.Tool;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagDao tagDao;

    @Autowired
    TokenService tokenService;

    @Override
    public ResultModel RecommendTag() {
        List<TagModel> tagList = tagDao.getRecommendTag();
        if (tagList.isEmpty()) {
            return ResultUtils.Error("没有数据");
        } else {
            return ResultUtils.Success(tagList);
        }
    }

    @Override
    public ResultModel OpusTag(Integer id) {
        log.info("作品的id为 ==== {}", id);
        List<TagModel> opusTag = tagDao.getOpusTag(id);
        if (opusTag.isEmpty()) {
            return ResultUtils.Error("该作品没有标签");
        } else {
            return ResultUtils.Success(opusTag);
        }
    }

    public ResultModel TagOpusList(String tags_title, Integer pageNum) {
        Page page = PageHelper.startPage(pageNum, 10);
        List<OpusModel> TagOpusArr = tagDao.getTagOpusList(tags_title);
        if (TagOpusArr.isEmpty()) {
            return ResultUtils.Error("该标签下没有作品");
        } else {
            return ResultUtils.Success(Tool.CountPage(page, TagOpusArr));
        }
    }

    @Override
    public ResultModel createdTag(String[] tag_list) {
        Integer user_id = tokenService.uid();
        Integer ins = tagDao.createdTag(user_id, tag_list);
        if (ins != 0) {
            return ResultUtils.SuccessStatus("创建成功");
        } else {
            return ResultUtils.Error("创建失败");
        }
    }

    @Override
    public ResultModel userTagList() {
        Integer user_id = tokenService.uid();
        List<TagModel> tagList = tagDao.getUserTagList(user_id);
        if (tagList.isEmpty()) {
            return ResultUtils.Error("该用户没有创建标签");
        } else {
            return ResultUtils.Success(tagList);
        }
    }

    @Override
    public ResultModel editTag(Integer tag_id, String title) {
        boolean isEdit = tagDao.editTag(tag_id, tokenService.uid(), title);
        if (isEdit) {
            return ResultUtils.SuccessStatus("标签编辑成功");
        } else {
            return ResultUtils.Error("标签编辑失败");
        }
    }

    @Override
    public ResultModel deleteTag(Integer tag_id) {
        boolean delete = tagDao.deleteTag(tag_id, tokenService.uid());
        if (delete) {
            return ResultUtils.SuccessStatus("标签删除成功");
        } else {
            return ResultUtils.Error("标签删除失败");
        }
    }

    @Override
    public ResultModel createdOpusInfoTag(CreatedOpusInfoTagModel tag) {
        Integer id = tagDao.addOpusInfoTag(tag);
        if (id != 0) {
            return ResultUtils.SuccessStatus("创建成功");
        } else {
            return ResultUtils.Error("创建失败");
        }
    }

    @Override
    public ResultModel UpdateTagsEditStatus(Map<String, Integer> tag) {
        Integer user_id = tokenService.uid();
        boolean updateId = tagDao.udateTagEditStatus(user_id, tag.get("opus_id"), tag.get("edit_status"));
        log.info("updateId ======================================================== {}: ", updateId);
        if (updateId) {
            return ResultUtils.SuccessStatus("改过状态成功");
        } else {
            return ResultUtils.Error("改过状态失败");
        }
    }
}
